
# EPONYM = IvanHoe-ZOG-MP
# VERSION = 999958l-ZOG-MP
EPONYM = IvanHoe-Beta
VERSION = 999952a-Beta
CC = gcc
FLAGS = -Wall
OPTIONS = -g -O3 -fstrict-aliasing -fomit-frame-pointer -fno-exceptions
# OPTIONS = -pg -O1

HAS = -DHAS_POPCNT # -DHAS_PREFETCH
HAS = # -DHAS_PREFETCH # comment out when HAS_POPCNT can avail
DEFINITIONS = -Dx86_64 $(HAS) -DVERSION=\"$(VERSION)\"
DEFINITIONS += -DEPONYM=\"$(EPONYM)\"
DEFINITIONS += -DCON_ROBBO_BUILD # RobboBases usage
# DEFINITIONS += -DNOME_WINDOWS # uncomment for RobboBuild with WINDOWS names

DEFINITIONS += -DYUSUF_MULTICORE # capitalist!
# DEFINITIONS += -DUSER_SPLIT # allow capitalist users to split nodes
# DEFINITIONS += -DMINIMAL # for MINIMAL
# DEFINITIONS += -DWINDOWS # for WINDOWS
# DEFINITIONS += -DDEBUG # validate
DEFINITIONS += -DBENCHMARK
# DEFINITIONS += -DTRACE_COMPILE
# DEFINITIONS += -DBUILD_ZOG_MP_COMPILE # active ZOG MP

LIBS = -lpthread

RobboLito_SOURCES = main.c \
                    arrays.c \
                    SLAB_MEMORY.c \
                    set_position.c \
                    utility.c \
                    material_value.c \
                    pawn_eval.c \
                    evaluation.c \
                    mobility.c \
                    50move.c \
                    MEM_HANDLER.c \
                    Zobrist_init.c \
                    Zobrist_use.c \
                    SEE.c \
                    move_gen.c \
                    make_move.c \
                    un_make_move.c \
                    top_node.c \
                    root_node.c \
                    pv_node.c \
                    cut_node.c \
                    all_node.c \
                    exclude_node.c \
                    control.c \
                    input.c \
                    search.c \
                    static.c \
                    next_move.c \
                    low_depth.c \
                    qsearch.c \
                    qsearch_pv.c \
                    ok_move.c \
                    Robbo_glue.c \
                    signals.c \
                    magic_mult.c

IVAN_SOURCES = SMP.c \
               SMP_init.c \
               SMP_search.c \
               top_analysis.c \
               root_analysis.c \
               root_multipv.c \
                benchmark.c \
#               root_zog_node.c \
#               ZOG_init.c \
#               ZOG_MP.c

RobboLito_SOURCES += $(IVAN_SOURCES)

UTILITY_SOURCES =  matval_explain.c \
                   eval_explain.c \
                   pawn_eval_explain.c \
                   perft.c \
                   validate.c

# DEFINITIONS += -DUTILITIES # perft # eval explain # Fact: EE slows 3%
# RobboLito_SOURCES += $(UTILITY_SOURCES) # comment out

RobboUsage_SOURCES = Robbo_utilita.c \
                     Robbo_cache.c \
                     Robbo_iniz.c \
                     Robbo_decomp.c \
                     RobboTriple_iniz.c \
                     RobboTriple.c \
                     RobboTripleValue.c \
                     Robbo_iniz_IO.c \
                     RobboTripleVerify.c
RobboUsage_HEADERS = RobboTotalBase.h \
                     RobboTripleBase.h

ROBBO_GLUE = $(RobboUsage_SOURCES:.c=.o)

RobboLito_HEADERS = RobboLito.h \
                    IH-win-linux.h \
                    IH-hash.h \
                    IH-ZOG-MP.h \
                    IH-SMP.h \
                    IH-board.h \
                    IH-move.h \
                    SLAB_MEMORY.h \
                    arrays.h \
                    bits.h \
                    functions.h \
                    control.h \
                    make_unmake.h \
                    white.h \
                    black.h \
                    undef.h \
                    common.h \
#                    ZOG_NODE.h

WINDOWS_HEADERS = win32bits.h \
                  win64bits.h
RobboLito_HEADERS += $(WINDOWS_HEADERS)

INCLUDE = init_gen.i \
          history.i \
          null_move.i \
          material_value.i

ALTRO = Makefile \
        YakovChart.bi \
        YakovChart.ne \
        evaluation.v \
        pawn_eval.v \
        .depend \
        READ_THIS \
        UCI_OPTIONS \
        triple3.sh \
        triple4.sh \
        triple5.sh \
        tripleZ.sh \
        CHANGE_LOG \
        COMRADES_HASH \
        LARGE_PAGES

RobboBuild_SOURCES = Robbo_gen.c \
                     Robbo_conto.c \
                     Robbo_utilita.c \
                     Robbo_cache.c \
                     Robbo_iniz.c \
                     Robbo_iniz_IO.c \
                     Robbo_mfa.c \
                     Robbo_build.c \
                     Robbo_comp.c \
                     Robbo_decomp.c \
                     RobboTripleBuild.c \
                     RobboTriple.c \
                     RobboTriple_iniz.c \
                     Robbo_popolare.c \
                     Robbo_mobilita.c \
                     Robbo_extra.c \
                     Robbo_suffix.c

RobboBuild_HEADERS = Robbo_gen_mossa.h \
                     RobboTotalBase.h \
                     RobboTripleBase.h

RobboLito_OBJECTS = $(RobboLito_SOURCES:.c=.o) $(ROBBO_GLUE)
RobboBuild_HACK =  arrays.o magic_mult.o SLAB_MEMORY.o MEM_HANDLER.o
RobboBuild_OBJECTS = $(RobboBuild_SOURCES:.c=.o) $(RobboBuild_HACK)
UTILITY_OBJECTS = $(UTILITY_SOURCES:.c=.o)

SOURCES = $(RobboLito_SOURCES) $(RobboBuild_SOURCES) \
          RobboTripleValue.c RobboTripleVerify.c $(UTILITY_SOURCES)
HEADERS = $(RobboLito_HEADERS) $(RobboBuild_HEADERS)

TUTTO = $(SOURCES) $(HEADERS) $(INCLUDE) $(ALTRO)

CP = /bin/cp
MKDIR = /bin/mkdir
RM = /bin/rm
SED = /bin/sed
TAR = /bin/tar
ALTRO_DIR = $(EPONYM)-$(VERSION)

CFLAGS += $(OPTIONS) $(FLAGS) $(DEFINITIONS)

all: RobboLito RobboBuild .depend Makefile
RobboLito: $(RobboLito_OBJECTS) $(RobboLito_HEADERS) Makefile
	$(CC) $(CFLAGS) -o $(EPONYM) $(RobboLito_OBJECTS) $(LIBS)
RobboBuild: $(RobboBuild_OBJECTS) $(RobboBuild_HEADERS) Makefile
	$(CC) $(CFLAGS) -o RobboBuild $(RobboBuild_OBJECTS) -lpthread
archive:
	$(MKDIR) $(ALTRO_DIR)
	$(CP) $(TUTTO) $(ALTRO_DIR)
	$(TAR) -cf $(EPONYM)-Backup-$(VERSION).tar $(ALTRO_DIR)/* $(ALTRO_DIR)/.depend
	$(RM) -rf $(ALTRO_DIR)
.depend: Makefile $(HEADERS)
	$(CC) -MM $(DEFINITIONS) $(SOURCES) | $(SED) 's/:/: Makefile/g' > $@
clean:
	$(RM) -f $(RobboLito_OBJECTS) $(RobboBuild_OBJECTS) $(EPONYM) RobboBuild

LD = /usr/bin/ld
OBJDUMP = /usr/bin/objdump
optimize: # not ready yet # idea for relocating symbols to reduct cache lames
	$(RM) -f $(EPONYM).ld
	$(LD) --verbose > LINKER
	$(CC) $(CFLAGS) -o Optimize Optimize.c
	$(OBJDUMP) -t $(EPONYM) | ./Optimize # changes LINKER + makes $(EPONYM).ld
	$(LD) -dT LINKER -O3 -e main -o $(EPONYM) $(RobboLito_OBJECTS) -lc $(LIBS) 

include .depend
